<?php
namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller;

class BaseController extends Controller
{
    /**
     * 基本控制器
     */

    public function __construct()
    {
    }

    /**
     * 返回APP端的Json数据
     * @param $code 错误代码
     * @param $data 返回的数据
     * @param $msg 自定义提示
     * return json
     */
    public function toAppJson($code=0, $data=array(), $msg='')
    {
        $msgArr = array(
            0 => '操作成功',
            -1 => '接口权限错误', -2 => '参数不全或错误',  -3 => 'TOKEN失败',
            -4 => '没有登陆',  -5 => '没有数据',  -6 => '新增失败',
            -7 => '历史新增错误',  -8 => '历史移除错误',  -9 => '验证码发送失败',
            -10 => '移除失败',  -11 => '手机号码格式错误',  -12 => '密码格式错误',
            -13 => '用户已存在',  -14 => '验证码错误',  -15 => '注册失败',
            -16 => '用户不存在',  -17 => '密码错误',  -18 => '建议新增失败',
            -19 => '上传失败',  -20 => '上传设置失败',  -21 => '已有数据',
            -22 => '',  -23 => '',  -24 => '',
            -25 => '',  -26 => '',  -27 => '',
            -28 => '',  -29 => '',  -30 => '',
        );
        $msgStr = array_key_exists($code,$msgArr) ? $msgArr[$code] : '请求失败';
        if ($msg) { $msgStr = $msgStr.'：'.$msg; }
        $jsonArr = array(
            'code' => $code,
            'msg' => $msgStr,
        );
        if ($data) { $jsonArr['data'] = $data; }
        return $jsonArr;
    }

    /**
     * 返回后台的Json数据
     * @param $code 错误代码
     * @param $data 返回的数据
     * @param $msg 自定义提示
     * return json
     */
    public function toAdminJson($code=0, $data=array(), $total=0, $msg='')
    {
        $msgArr = array(
            0 => '操作成功', -500 => '操作失败',
            -1 => '接口权限错误', -2 => '参数不全或错误', -3 => '没有数据',
            -4 => '存在重复数据', -5 => '', -6 => '',
            -7 => '', -8 => '', -9 => '',
        );
        $msgStr = array_key_exists($code,$msgArr) ? $msgArr[$code] : '请求失败';
        if ($msg) { $msgStr = $msgStr.'：'.$msg; }
        $jsonArr = array(
            'code' => $code,
            'msg' => $msgStr,
            'total' => $total,
        );
        if ($data) { $jsonArr['data'] = $data; }
        return $jsonArr;
    }
}